package com.xiangwushuo.framework.activity.start

import android.Manifest
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.lib.common.base.mvvm.CommonViewModelActivity
import com.lib.common.repository.common.ComSharePreHelper
import com.lib.common.repository.common.SpKeyInComSp
import com.lib.common.repository.datacenter.DataCenter
import com.lib.common.widget.dialog.AgreementDialog
import com.lib.framework.entity.Event
import com.lib.framework.ext.startActivity
import com.lib.framework.helper.permission.PermissionHelper
import com.lib.framework.manager.ContextManager
import com.lib.framework.manager.LogManager
import com.lib.framework.widget.WidgetUtil
import com.lib.framework.widget.dialog.OperatePromptDialog
import com.xiangwushuo.framework.AppInitHelper
import com.xiangwushuo.framework.R
import com.xiangwushuo.framework.activity.main.MainActivity
import com.xiangwushuo.framework.databinding.ActivityStartBinding

/**
 * Copyright (C), 2019-2019
 * FileName: StartActivity
 * Author: Kevin
 * Date: 2019-12-31 11:23
 * Description:
 * History: 启动页
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class StartActivity : CommonViewModelActivity<StartViewModel>() {

    companion object {

        /**
         * 发给 activity 的事件
         */
        const val EVENT_XXX = 1 // XXX 事件
    }

    private var mDataBinding: ActivityStartBinding? = null

    /**
     * 协议对话框
     */
    private var mAgreementDialog: AgreementDialog? = null

    /**
     * 是否进行了 app 初始化
     */
    private var mIsAppInit = false

    override fun getViewModelClass() = StartViewModel::class.java

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_start)
        mDataBinding?.viewModel = mViewModel

        initView()
    }

    /**
     * 初始化视图
     */
    private fun initView() {
        if (!ComSharePreHelper.getBoolean(SpKeyInComSp.SP_KEY_AGREED_AGREEMENT, false)) {
            showAgreementDialog()
        } else {
            appInitAfterAgreedAgreement()
        }
    }

    /**
     * 同意协议之后 的应用初始化
     */
    private fun appInitAfterAgreedAgreement() {
        if (mIsAppInit) return
        mIsAppInit = true

        AppInitHelper.initInStartActvity(this.applicationContext)
        PermissionHelper.requestPermissions(
            this,
            object :
                PermissionHelper.OnPermissionListener {
                override fun onGranted() {
                    permissionApplyFinish()
                }

                override fun onDenied() {
                    permissionApplyFinish()
                }
            },
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )
    }

    /**
     * 权限申请完成
     */
    private fun permissionApplyFinish() {
        AppInitHelper.initAfterAgreedAgreement(ContextManager.applicationContext)

        mViewModel.goToNextPage()
    }


    override fun dealEvent(event: Event?): Boolean {
        // 处理 viewModel 发送过来的事件
        when (event?.code) {
            EVENT_XXX -> {
                val extraObj = event.extraObj
                if (extraObj is String) {
                    LogManager.i(mTag, "XXX 事件 的额外参数 extraObj = $extraObj")
                }
                return true
            }
        }

        return super.dealEvent(event)
    }

    /**
     * 显示 协议对话框
     */
    private fun showAgreementDialog() {
        closeAgreementDialog()
        mAgreementDialog = AgreementDialog(this)
        mAgreementDialog?.mCallback = object : AgreementDialog.Callback {
            override fun onAgreed() {
                ComSharePreHelper.putBoolean(SpKeyInComSp.SP_KEY_AGREED_AGREEMENT, true)
                appInitAfterAgreedAgreement()
            }

            override fun onNoAgreed() {
                finish()
            }
        }

        mAgreementDialog?.show()
    }

    /**
     * 关闭 协议对话框
     */
    private fun closeAgreementDialog() {
        mAgreementDialog?.cancel()
        mAgreementDialog = null
    }

    override fun onDestroy() {
        super.onDestroy()
        closeAgreementDialog()
    }
}
